package com.ys.controller.api.logistics;

import com.ys.config.MenuId;
import com.ys.entity.logistics.TpAmazonAddress;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.logistics.TpAmazonAddressService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService.UploadDto;
import com.ys.util.ExportExcelUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/*
 * 亚马逊仓库地址管理
 */
@Controller
@RequestMapping("/api/inter/tpAmazonAddress")
public class ApiInterTpAmazonAddressController {
	private static final Logger logger = LoggerFactory.getLogger(ApiInterTpAmazonAddressController.class);

	@Autowired
	private CommonApiService<TpAmazonAddress> commonApiService;


	@Autowired
	private TpAmazonAddressService tpAmazonAddressService;
	
	@RequiresPermissions("tpAmazonAddress:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(@ModelAttribute TpAmazonAddress cond,
			String conditions){
		cond.addLeftJoin("TB_Country B ON B.code =A.CountryCode")
			.addField("B.Name countryName,B.Code countryCode");
		cond = setSeachSql(cond, conditions);
	
		return commonApiService.searchLay(cond);
	}

	/*
	 * 新增
	 */
	@RequestMapping("/toAdd")
	@ResponseBody
	public R toAdd(HttpServletRequest request, @RequestParam Map<String, Object> params, Long menuId
	) {
		try {
			logger.info("费用返还单新增传参：" + params);
			//修改
			return tpAmazonAddressService.add(params,menuId);
		} catch (Exception e) {
			logger.error("费用返还单新增出错：", e.getMessage(), e);
			return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
		}
	}

	/**
	 * 修改
	 */
	@RequestMapping("/toEdit")
	@ResponseBody
	public R toEdit(HttpServletRequest request, @RequestParam Map<String, Object> params, Long menuId
	) {
		try {
			logger.info("费用返还单修改传参：" + params);
			//修改
			return tpAmazonAddressService.update(params,menuId);
		} catch (Exception e) {
			logger.error("费用返还单修改出错：", e.getMessage(), e);
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
		}
	}


	/**
	 * 删除
	 * @throws Exception
	 */
	@PostMapping("/delete")
	@ResponseBody
	public R delete(Long id){

		try {
			return tpAmazonAddressService.del(id);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("操作失败，请联系客服");
		}
	}
	
	/*
	 * 导出
	 */
	@RequiresPermissions("tpAmazonAddress:export")
	@RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TpAmazonAddress cond,
			String conditions) throws Exception{
		
		cond = setSeachSql(cond, conditions);
		
		cond.setOrderby("A.Id DESC");
		
		cond.setPage(0).setSize(999999);

		List<Map<String, Object>> listData = cond.queryMap();
        
    	List<YsCol> listField = new YsCol()
    			.setMenuId(MenuId.TP_AmazonAddress)
    			.setFrontShow(YsColEnum.FrontShow.SHOW.value)
    			.setOrderby("A.SortNo asc")
    			.queryList();
    	
    	
        ExportExcelUtils<TpAmazonAddress> util = new ExportExcelUtils<TpAmazonAddress>(new TpAmazonAddress());
        
        String toFilePath = util.exportExcel(listData, listField, "亚马逊仓库地址管理");
        UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
        return R.ok(dto.getUrl());
    }

    /**
     * 设置查询的cond的sql设置
     * @param cond
     * @return
     */
    public TpAmazonAddress setSeachSql(TpAmazonAddress cond, String conditions){
    	
    	cond.//排序
        setOrderby("A.Id DESC");
		
        return  cond;
    }
}
